#include "PCH.h"
#include "..\Common.h"

/*
 * SP1 = Standard Parallel 1, SP2 = Standard Parallel 2
 * lat = latitude of origin, lon = longitude of origin
 */
AlbersToLatLon::AlbersToLatLon(double nSP1, double nSP2, double nlat, double nlon) {

	double SP1rad = 0, SP2rad = 0, latrad = 0;
	SP1rad = deg2rad(nSP1);
	SP2rad = deg2rad(nSP2);
	latrad = deg2rad(nlat);
	lon = deg2rad(nlon);
	
	n = (sin(SP1rad) + sin(SP2rad))/2; //(14-6)
	C = (cos(SP1rad)*cos(SP1rad)) + 2 * n * sin(SP1rad); //(14-5)
	P0 = R * pow((C - 2*n*sin(latrad)), .5) / n; //(14-3a)

}

void AlbersToLatLon::Convert(double x, double y, double *newx, double *newy) {
	double lat = 0, lon = 0;
	double p = pow((x*x) + (P0*P0 - 2*y*P0 + y*y), .5);
	double theta = atan(x/(P0-y));
	
	lon = this->lon + theta/n;
	double twork = ((p*n)/R);
	twork = twork*twork;
	twork = C - twork;
	twork = twork/(2*n);
	lat = asin(twork);
	*newy = rad2deg(lon);
	*newx = rad2deg(lat);
}